$(function () {
    layer.config({

        extend: 'extend/layer.ext.js',
        skin: 'orangeLayer'

    });

    //暂无记录
    if( $('.table_list li').length <= 0 ) {
        var html = "<div style='text-align: center;padding: 10px 0;'><p>暂无记录！</p></div>";
        $('.table_header').after(html);
    }

    //按分类搜索后，还原搜索条件
    if( $('#typeId').val() != 0) {
        var typeId = $('#typeId').val();
        $('.selTypeBtn span').first().text($('#'+typeId).text());
    }
    else {
        $('.selTypeBtn span').first().text('选择分类');
    }

    // 资讯列表删除
    $('.info_list_module').on('click', '.delete', function (event) {
        var ids = {
            ids : $(this).attr("data-id")
        }
        var $table_tr = $(this).parent().parent(),
            dataUrl = $(this).attr('href');
        if ($('.info_list_module').hasClass('info_recycle_module')) {
            var msg = '您确定要彻底删除此资讯吗？';
        }
        else {
            var msg = '您确定要将此资讯放入回收站吗？';
        }
        layer.confirm(msg, {
            title: '提示',
            skin: 'orangeLayer'
        }, function (index) {
            layer.msg('请稍候...', {icon: 16,time: 0});
            $.post(dataUrl,ids, function (result) {
                var res = $.parseJSON(result);
                if (res.succ) {
                    layer.msg('删除成功', {
                        time: 1000
                    }, function(){
                        window.location.reload();
                    });
                }
                else {
                    layer.msg(res.msg);
                }
            }).error(function () {
                layer.msg('系统或参数错误!!!');
            });
            //$table_tr.remove();
            layer.close(index);
        }, function () {

        });

        return false;
    });


    //aritcle_add 上传图片事件
    $('#uploadImg').change(function (event) {
        var file = $(this).get(0).files[0];
        var fileSize = 0;
        fileSize = Math.round(file.size * 100 / 1024) / 100;
        fileSizeStr = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
        if (fileSize > 2048) {
            layer.msg('图片大小不能超过2M');
            delImg();
            return false;
        }
        else {
            $('.img_info').text(file.name + ' / ' + fileSizeStr);
            return true;
        }
    });

    //全选复选框
    $('.total_sel').click(function () {
        if ($(this).is(':checked')) {
            $('.operate_sel').addClass('selected');
        }
        else {
            $('.operate_sel').removeClass('selected');
        }
        $('.table_list .table_tr').each(function () {
            if ($(this).css('display') == 'list-item') {
                if ($('.operate_sel').hasClass('selected')) {
                    $(this).find('.info_sel').prop('checked', true);
                }
                else {
                    $(this).find('.info_sel').removeAttr('checked');
                }
            }
        });
    });

    //全选按钮
    $('.operate_sel').click(function () {
        $(this).toggleClass('selected');
        $('.table_list .table_tr').each(function () {
            if ($(this).css('display') == 'list-item') {
                if ($('.operate_sel').hasClass('selected')) {
                    $(this).find('.info_sel').prop('checked', true);
                    $('.total_sel').prop('checked', true);
                }
                else {
                    $(this).find('.info_sel').removeAttr('checked');
                    $('.total_sel').removeAttr('checked');
                }
            }
        });
        return false;
    });

    //全选清空
    $('.pages').on('click', 'a', function () {
        $('.table_list .table_tr .info_sel').removeAttr('checked');
        $('.total_sel').removeAttr('checked');
        $('.operate_sel').removeClass('selected');
    });

    // 批量 删除
    $('.operate_del').on('click', function () {
        var dataUrl = $(this).attr('href');
        var count = 0;
        var idArr = new Array();
        $('.info_sel').each(function (index) {
            if ($(this).is(':checked')) {
                idArr.push($(this).attr('id').match(/\d+/));
                count += 1;
            }
        });
        if (count > 0) {
            var idStr = 'ids=' + idArr.join(',');
            layer.confirm('您确定要删除这' + count + '条记录吗？', {
                title: '提示',
                skin: 'orangeLayer'
            }, function (index) {
                layer.msg('请稍候...', {icon: 16,time: 0});
                $.post(dataUrl, idStr, function (result) {
                    var res = $.parseJSON(result);
                    if (res.succ) {
                        layer.msg('删除成功', {
                            time: 1000
                        }, function(){
                            location.reload();
                        });
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }).error(function () {
                    layer.msg('系统或参数错误!!!');
                });
                layer.close(index);
            }, function () {

            });
        }
        else {
            layer.msg('请选择要删除的记录');
        }
        return false;
    });

    // 批量 推荐
    $('.operate_rec').on('click', function () {
        var dataUrl = $(this).attr('href');
        var count = 0;
        var idArr = new Array();
        $('.info_sel').each(function (index) {
            if ($(this).is(':checked')) {
                idArr.push($(this).attr('id').match(/\d+/));
                count += 1;
            }
        });
        if (count > 0) {
            var idStr = 'ids=' + idArr.join(',');
            layer.confirm('您确定要推荐这' + count + '条记录吗？', {
                title: '提示',
                skin: 'orangeLayer'
            }, function (index) {
                layer.msg('请稍候...', {icon: 16,time: 0});
                $.post(dataUrl, idStr, function (result) {
                    var res = $.parseJSON(result);
                    if (res.succ) {
                        layer.msg('推荐成功', {
                            time: 1000
                        }, function(){
                            window.location.reload();
                        });
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }).error(function () {
                    layer.msg('系统或参数错误!!!');
                });
                layer.close(index);
            }, function () {

            });
        }
        else {
            layer.msg('请选择要推荐的记录');
        }
        return false;
    });

    // 导出
    $('.operate_export').on('click', function () {
        var dataUrl = $(this).data("href");
        var data = $("#info_search").serialize();

        // 获得url和data
        if( dataUrl && data ){
            // data 是 string 或者 array/object
            data = typeof data == 'string' ? data : jQuery.param(data);
            // 把参数组装成 form的  input
            var inputs = '';
            jQuery.each(data.split('&'), function(){
                var pair = this.split('=');
                inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ decodeURI(pair[1]) +'" />';
            });
            
            // request发送请求
            jQuery('<form action="'+ dataUrl +'" method="post">'+inputs+'</form>')
                .appendTo('body').submit().remove();
        }

        return false;
    });

    // 批量 还原 资讯
    $('.operate_reduce').on('click', function () {
        var dataUrl = $(this).attr('href');
        var count = 0;
        var idArr = new Array();
        $('.info_sel').each(function (index) {
            if ($(this).is(':checked')) {
                idArr.push($(this).attr('id').match(/\d+/));
                count += 1;
            }
        });
        if (count > 0) {
            var idStr = 'ids=' + idArr.join(',');
            layer.confirm('您确定要还原这' + count + '条资讯吗？', {
                title: '提示',
                skin: 'orangeLayer'
            }, function (index) {
                layer.msg('请稍候...', {icon: 16,time: 0});
                $.post(dataUrl, idStr, function (result) {
                    var res = $.parseJSON(result);
                    if (res.succ) {
                        layer.msg('还原成功', {
                            time: 1000
                        }, function(){
                            window.location.reload();
                        });
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }).error(function () {
                    layer.msg('系统或参数错误!!!');
                });
                layer.close(index);
            }, function () {

            });
        }
        else {
            layer.msg('请选择要还原的记录');
        }
        return false;
    });

    //添加、修改等级分类
    $('.info_level_module').on('click', '.layer-btn', function (e) {
        var ids = {
            ids : $(this).attr("data-id")
        }
        var title,
            types,
            temp,
            layer_data = {};
        if ($(this).hasClass('add-btn')) {
            title = '添加等级分类';
            types = 1;
            var dataUrl = '/sys/information/article/level/add';
            layer_data = {
                dataUrl: dataUrl
            };
        }
        else if ($(this).hasClass('operate')) {
            title = '修改等级分类';
            types = 2;
            var $table_tr = $(this).parent().parent();
            var level = $table_tr.children('.info_item2').text();
            var name = $table_tr.children('.info_item3').text();
            var introduce = $table_tr.children('.info_item4').text();
            var dataUrl = $(this).attr('href');
            var idNum = parseInt($(this).attr('id').match(/\d+/)); //记录id
            layer_data = {
                idNum: idNum,
                level: level,
                title: name,
                remark: introduce,
                dataUrl: dataUrl
            };
        }
        else if ($(this).hasClass('delete')) {
            var $table_tr = $(this).parent().parent(),
                dataUrl = $(this).attr('href');
            layer.confirm('您确定要删除此等级分类吗？', {
                title: '提示',
                skin: 'orangeLayer'
            }, function (index) {
                layer.msg('请稍候...', {icon: 16,time: 0});
                $.post(dataUrl, ids, function (result) {
                    var res = $.parseJSON(result);
                    if (res.retCode == "success") {
                        layer.msg('删除成功', {
                            time: 1000
                        }, function(){
                            window.location.reload();
                        });
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }).error(function () {
                    layer.msg('系统或参数错误!!!');
                });
                layer.close(index);
            }, function () {

            });
            return false;
        }
        temp = template('addLevelModule', layer_data);
        layer.open({
            title: title,
            type: 1, //页面层需要手动关闭
            area: '600px',
            closeBtn: 1,
            content: temp,
            success: function (index) {
                $('#addLevelForm').validate({
                    rules: {
                        name: "required",
                        introduce: "required"
                    },
                    messages: {
                        name: "分类名称不能为空",
                        introduce: "简述不能为空"
                    },
                    submitHandler: function (form) {
                        e.preventDefault();
                        var data = $(form).serialize();
                        var dataUrl = $(form).attr('action');
                        if (types == 1) {
                            layer.msg('请稍候...', {icon: 16,time: 0});
                            $.post(dataUrl, data, function (result, status) {
                                result = $.parseJSON(result);
                                if( result.succ ) {
                                    layer.msg('操作成功', {
                                        time: 1000
                                    }, function(){
                                        window.location.reload();
                                    });
                                }
                                else {
                                    layer.msg(result.msg);
                                }
                            }).error(function () {
                                layer.msg('系统或参数错误!!!');
                            });
                        }
                        else if (types == 2) {
                            layer.msg('请稍候...', {icon: 16,time: 0});
                            $.post(dataUrl, data, function (result, status) {
                                result = $.parseJSON(result);
                                if( result.succ ) {
                                    layer.msg('操作成功', {
                                        time: 1000
                                    }, function(){
                                        window.location.reload();
                                    });
                                }
                                else {
                                    layer.msg(result.msg);
                                }
                                //var level = $('#level').val();
                                //var title = $('#title').val();
                                //var remark = $('#remark').val();
                                //$table_tr.children('.info_item2').text(level);
                                //$table_tr.children('.info_item3').text(title);
                                //$table_tr.children('.info_item4').text(remark);
                                //layer.closeAll();
                                //layer.msg('修改成功');
                            }).error(function () {
                                layer.msg('系统或参数错误!!!');
                            });
                        }
                    }
                });
            }
        });
        return false;
    });

    //添加、修改服务申请列表
    $('.info_matching_module').on('click', '.layer-btn', function (e) {
        e.preventDefault();
        var title,
            types,
            temp,
            layer_data = {};
        if ($(this).hasClass('common-btn-gray')) {
            title = '添加配套分类';
            types = 1;
            layer_data = {
                dataUrl: '/sys/information/supportype/add'
            };
        }
        else if ($(this).hasClass('operate')) {
            title = '修改配套分类';
            types = 2;
            var $table_tr = $(this).parent().parent();
            var idNum = parseInt($(this).attr('id').match(/\d+/));
            var name = $table_tr.children('.infoSort_item2').text();
            layer_data = {
                idNum: idNum,
                name: name,
                dataUrl: '/sys/information/supportype/update'
            };
        }
        else if ($(this).hasClass('delete')) {
            var $table_tr = $(this).parent().parent(),
                dataUrl = $(this).attr('href');
            var ids = {ids:$(this).attr("data-id")};
            layer.confirm('您确定要删除此申请吗？', {
                title: '提示',
                skin: 'orangeLayer'
            }, function (index) {
                layer.msg('请稍候...', {icon: 16,time: 0});
                $.get(dataUrl,ids, function (result) {
                    var res = $.parseJSON(result);
                    if (res.succ) {
                        //layer.msg('删除成功');
                        layer.msg('删除成功', {
                            time: 1000
                        }, function(){
                            window.location.reload();
                        });
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }).error(function () {
                    layer.msg('系统或参数错误!!!');
                });
                layer.close(index);
            }, function () {

            });
            return false;
        }
        else if ($(this).hasClass('deleteClass')) {
            var $table_tr = $(this).parent().parent(),
                dataUrl = $(this).attr('href');
            var ids = {ids:$(this).attr("data-id")};
            layer.confirm('您确定要删除此分类吗？', {
                title: '提示',
                skin: 'orangeLayer'
            }, function (index) {
                layer.msg('请稍候...', {icon: 16,time: 0});
                $.get(dataUrl,ids, function (result) {
                    var res = $.parseJSON(result);
                    if (res.succ) {
                        //layer.msg('删除成功');
                        layer.msg('删除成功', {
                            time: 1000
                        }, function(){
                            window.location.reload();
                        });
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }).error(function () {
                    layer.msg('系统或参数错误!!!');
                });
                layer.close(index);
            }, function () {

            });
            return false;
        }
        temp = template('sortModule', layer_data);
        layer.open({
            title: title,
            type: 1, //页面层需要手动关闭
            area: '600px',
            closeBtn: 1,
            content: temp,
            success: function (index) {
                $('#sortForm').validate({
                    rules: {
                        name: "required"
                    },
                    messages: {
                        name: "分类名称不能为空"
                    },
                    submitHandler: function (form) {
                        //console.log(form);
                        if (document.all) {
                            event.returnValue = false;
                        } else {
                            e.preventDefault();
                        };
                        var dataUrl = $(form).attr("action");
                        var data = $(form).serialize();
                        //if (types == 1) {
                        //    var d = new Date();
                        //    var $new = $('.table_tr:first-child').clone();
                        //    $new.children('.infoSort_item2').text(name);
                        //    $new.children('.infoSort_item3').text('0');
                        //    $new.children('.infoSort_item4').text(d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate());
                        //    $('#infoList').append($new);
                        //}
                        //else if (types == 2) {
                        //    $table_tr.children('.infoSort_item2').text(name);
                        //}
                        layer.msg('请稍候...', {icon: 16,time: 0});
                        $.post(dataUrl, data, function(result){
                            result = $.parseJSON(result);
                            if( result.succ ) {
                                layer.msg('操作成功', {
                                    time: 1000
                                }, function(){
                                    window.location.reload();
                                });
                            }
                            else {
                                layer.msg(result.msg);
                            }
                        }).error(function(){
                            layer.msg("参数或系统错误！！！");
                        });
                        layer.closeAll();
                    }
                });
            }
        });
        return false;
    });

    //添加、修改帮助列表
    $('.info_helpList_module').on('click', '.layer-btn', function (e) {
        e.preventDefault();
        var title,
            types,
            temp,
            layer_data = {};
        if ($(this).hasClass('common-btn-gray')) {
            title = '添加';
            types = 1;
            layer_data = {
              dataUrl: '/sys/information/help/add'
            };
        }
        else if ($(this).hasClass('operate')) {
            title = '修改';
            types = 2;
            var $table_tr = $(this).parent().parent();
            var id = $table_tr.find(".info_sel").attr("id").replace("info_sel_","");
            var imgPath = $table_tr.find('.imgPath').val();
            var classify = $table_tr.children('.info_item2').text();
            var name = $table_tr.children('.info_item3').text();
            var url = $table_tr.children('.url').val();
            var dataUrl = $(this).attr('href');
            var typeId = '';
            $('.img_view').attr('src', imgPath); //用于预览
            $.ajaxSettings.async = false;
            $.get(dataUrl, function(result){
                result = $.parseJSON(result);
                typeId = result.item.typeId;
                layer_data = {
                    classify: classify,
                    id:id,
                    name: name,
                    imgPath:imgPath,
                    content: result.item.content,
                    url : url,
                    dataUrl: '/sys/information/help/modify',
                    type : typeId
                };
            });
        }
        else if ($(this).hasClass('detail')) {
            title = '查看';
            types = 3;
            var $table_tr = $(this).parent().parent();
            var imgPath = $table_tr.find('.imgPath').val();
            var classify = $table_tr.children('.info_item2').text();
            var id = $table_tr.find(".info_sel").attr("id").replace("info_sel_","");
            var name = $table_tr.children('.info_item3').text();
            var dataUrl = $(this).attr('href');
            $.ajaxSettings.async = false;
            $.get(dataUrl, function(result){
                result = $.parseJSON(result);
                layer_data = {
                    classify: classify,
                    id:id,
                    name: name,
                    imgPath:imgPath,
                    content: result.item.content,
                    dataUrl: '/sys/information/help/modify'
                };
            });
        }
        else if ($(this).hasClass('delete')) {
            var $table_tr = $(this).parent().parent(),
                dataUrl = $(this).attr('href');
            layer.confirm('您确定要删除此内容吗？', {
                title: '提示',
                skin: 'orangeLayer'
            }, function (index) {
                layer.msg('请稍候...', {icon: 16,time: 0});
                 $.get(dataUrl, function(result){
                     result = $.parseJSON(result);
                     if( result.succ ) {
                         layer.msg('删除成功', {
                             time: 1000
                         }, function(){
                             window.location.reload();
                         });
                     }
                     else {
                         layer.msg(result.msg);
                     }
                 }).error(function(){
                     layer.msg('系统或参数错误!!!');
                 });
                layer.close(index);
            }, function () {

            });
            return false;
        }
        temp = template('helpListModule', layer_data);
        layer.open({
            title: title,
            type: 1, //页面层需要手动关闭
            area: '800px',
            offset: '60px',
            closeBtn: 1,
            content: temp,
            success: function (index) {
                //还原分类选项
                if( typeId != undefined ) {
                    $('.tempB span').first().text($('#'+typeId).text());
                }
                //选择分类事件绑定
                $('.tempB').click(function(){
                    var x = $(this).offset();
                    var top = x.top + 30;
                    var left = x.left;
                    $('.typeMenu').slideToggle();
                    $('.typeMenu').offset({top: top, left: left});
                    $('.subTypeMenu').hide();
                    return false;
                });
                $('.helpListTypeMenu').on('click', 'a', function(){
                    $('.tempB span').first().text($(this).text());
                    $('#tempTypeId').val($(this).attr('id'));
                    $('.tempM').slideUp();
                    $('.subTypeMenu').hide();
                    return false;
                });
                $('.sel_class_module').hover(function(){
                },function(){
                    $('.subTypeMenu').hide();
                });

                //file upload 按钮绑定
                $(".title_img_module").on("change", "#uploadImg", function (event) {
                    /* 图片信息显示 */
                    var file = $(this).get(0).files[0];
                    var fileSize = Math.round(file.size * 100 / 1024) / 100;
                    var fileSizeStr = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
                    if (fileSize > 2048) {
                        layer.msg('图片大小不能超过2M');
                        delImg();
                        return false;
                    }
                    $('.img_info').text(file.name+' / '+fileSizeStr);
                    /* 图片信息显示 */

                    /* 图片预览处理*/
                    //获取图片路径
                    // Get a reference to the fileList
                    var files = !!this.files ? this.files : [];

                    // If no files were selected, or no FileReader support, return
                    if (!files.length || !window.FileReader) return;

                    // Only proceed if the selected file is an image
                    if (/^image/.test( files[0].type)){

                        // Create a new instance of the FileReader
                        var reader = new FileReader();

                        // Read the local file as a DataURL
                        reader.readAsDataURL(files[0]);

                        // When loaded, set image data as background of div
                        reader.onloadend = function(){

                            $(".img_view").attr("src",this.result);

                        }

                    }
                    /* 图片预览处理*/

                    photoUpload(event,"titleFile");
                });

                //加载富文本框
//                CKEDITOR.replace('introduce', {
//                    width: 650,
//                    height: 200
//                });
                
                window.helpeditor = KindEditor.create('textarea[name="content"]',  {
                    width: '680px',
                    height: '300px',
                    resizeType : 2,
                    zIndex : 999999999,
                    fieldName:'file', //上传input的NAME
                    uploadJson : '/upload/' + $("#photoDir").val() + '/imgFile/editor_up_json' // 文件上传调用的后台方法
                });
                
                $("#submit1").on("click", function() {
                    helpeditor.sync();
                    var id = $("#article-id").val();
                    var tempTypeId = $("#tempTypeId").val();
                    var title = $("#name").val();
                    var uploadImg = $("#uploadImgUrl").val();
                    var url = $("#url").val();
                    var content = helpeditor.html();
                    
                    if (tempTypeId == "0") {
                        layer.msg('请选择分类');
                        return false;
                    } else if (title == "") {
                        layer.msg('请填写标题');
                        return false;
                    } else if (content == "") {
                        layer.msg('请填写内容');
                        return false;
                    }
                    
                    $.ajax({
                        type     : "post",
                        dataType : "json",
                        url      : $("#helpListForm").attr("action"),
                        data : {
                            "id"      : id,
                            "typeId"  : tempTypeId,
                            "title"   : title,
                            "imgPath" : uploadImg,
                            "url" : url,
                            "content" : content
                        },
                        success : function(result) {
                            if (result.succ) {
                                layer.msg('操作成功', { time: 1000 }, function(){
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error : function() {
                            layer.msg('系统或参数错误!!!');
                        }
                    });
                    
                });
                
                //验证表单
//                $('#helpListForm').validate({
//                    rules: {
//                        name: "required"
//                    },
//                    messages: {
//                        name: "分类名称不能为空"
//                    },
//                    submitHandler: function (form) {
//                        if (document.all) {
//                            event.returnValue = false;
//                        } else {
//                            e.preventDefault();
//                        };
//                        var classify = $('#classify').val();
//                        var name = $('#name').val();
//                        var imgFile = $('.img_info ').html();
//                        CKEDITOR.instances.introduce.updateElement();
//                        var content = CKEDITOR.instances.introduce.getData();
//                        
//                        if ($("#tempTypeId").val() == "0") {
//                            layer.msg('请选择分类');
//                        }
//                        
////                        if (imgFile == '') {
////                            layer.msg('请上传图片');
////                        }
////                        else
//                            
//                        else if (content == '') {
//                            layer.msg('请填写内容');
//                        }
//                        else {
//                            var dataUrl = $(form).attr('action');
//                            var data = $(form).serialize();
////                            data = data + '&content='+content;
//                            
////                            console.log(data);
////                            return false;
//                            
//                            //data = decodeURIComponent(data);
//                            layer.msg('请稍候...', {icon: 16,time: 0});
//                            $.post(dataUrl, data, function(result){
//                                result = $.parseJSON(result);
//                                if( result.succ ) {
//                                    layer.msg('操作成功', {
//                                        time: 1000
//                                    }, function(){
//                                        window.location.reload();
//                                    });
//                                }
//                                else {
//                                    layer.msg(result.msg);
//                                }
//                            }).error(function(){
//                                layer.msg('系统或参数错误!!!');
//                            });
//                            //var d = new Date();
//                            //if (types == 1) {
//                            //    var $new = $('#infoList li:first-child').clone();
//                            //    $new.children('.info_item2').text(classify);
//                            //    $new.children('.info_item3').text(name);
//                            //    $new.children('.info_item4').text(d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate());
//                            //    $('#infoList').append($new);
//                            //    postWithLoading("/sys/information/help/add",$(form).serialize(),function(data){
//                            //        layer.closeAll();
//                            //    },function(){
//                            //
//                            //    });
//                            //
//                            //}
//                            //else if (types == 2) {
//                            //    $table_tr.children('.info_item2').text(classify);
//                            //    $table_tr.children('.info_item3').text(name);
//                            //    $table_tr.children('.info_item4').text(d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate());
//                            //    postWithLoading("/sys/information/help/modify",$(form).serialize(),function(data){
//                            //        layer.closeAll();
//                            //    },function(){
//                            //
//                            //    });
//                            //} else{
//                            //    layer.closeAll();
//                            //}
//                        }
//                    }
//                });
            },
            cancel: function(index) {
                //$('.mark').show();
            },
        });
        return false;
    });

    //分类管理交互
    $('.info_main').on('click', '.collapse', function () {
        var $collapse = $(this);
        if( $collapse.parent().parent().parent().parent().find('.info_subClass').length > 0 ) {
            var $curtain = $(this).parent().parent().parent().parent().find('.info_subClass');
            if ($curtain.css('display') == 'none') {
                $collapse.text('收起分类 Λ');
            }
            else {
                $collapse.text('展开分类 V');
            }
            $curtain.slideToggle();
        }
        return false;
    });

    //添加、修改资讯分类
    $('.info_newsClassify_module').on('click', '.layer-btn', function (e) {
        e.preventDefault();
        var title, content;
        if ($(this).hasClass('add_subClass')) {
            title = '添加分类';
        }
        else if ($(this).hasClass('modify')) {
            title = '修改分类';
        }
        else if ($(this).hasClass('delete')) {
            var dataUrl = $(this).attr('href');
            layer.confirm('您确定要删除此分类吗？', {
                title: '删除分类',
                skin: 'orangeLayer'
            }, function (index) {
                layer.msg('请稍候...', {icon: 16,time: 0});
                $.get(dataUrl, function (result) {
                    var res = $.parseJSON(result);
                    if (res.succ) {
                        layer.msg('删除成功', {
                            time: 1000
                        }, function(){
                            window.location.reload();
                        });
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }).error(function () {
                    layer.msg('系统或参数错误!!!');
                });
                layer.close(index);
            }, function () {

            });
            return false;
        } else {
            return false;
        }

        $.ajaxSettings.async = false;
        $.get(this.href, function(result){
            content = result; // 获取弹窗
        });
        layer.open({
            title: title,
            type: 1, //页面层需要手动关闭
            area: ['530px', 'auto'],
            offset: '150px',
            closeBtn: 1,
            content: content,
            success: function (index) {
                $('#classifyForm').validate({
                    rules: {
                        name: "required"
                    },
                    messages: {
                        name: "分类名称不能为空"
                    },
                    submitHandler: function (form) {
                        //event.preventDefault();
                        if (document.all) {
                            event.returnValue = false;
                        } else {
                            e.preventDefault();
                        };
                        var dataUrl = $(form).attr('action');
                        var data = $(form).serialize();
                        layer.msg('请稍候...', {icon: 16,time: 0});
                        $.post(dataUrl, data, function(result, status){
                            result = $.parseJSON(result);
                            if( result.succ ) {
                                layer.msg('操作成功', {
                                    time: 1000
                                }, function(){
                                    window.location.reload();
                                });
                            }
                            else {
                                layer.msg(result.msg);
                            }
                        }).error(function(){
                            layer.msg('系统或参数错误!!!');
                        });
                    }
                });
            }
        });
        return false;
    });

    //添加、修改帮助分类
    $('.info_helpClassify_module').on('click', '.layer-btn', function (e) {
        var title,
            temp,
            layer_data = {};
        if ($(this).hasClass('add_subClass')) {
            title = '添加分类';
            var addTypes = parseInt($(this).attr('data-floor')) + 1,
                parentKey = parseInt($(this).attr('id').match(/\d+/));
            layer_data = {
                floorKey: addTypes,
                parentKey: parentKey,
                dataUrl: '/sys/information/help/type/add'
            }
        }
        else if ($(this).hasClass('modify')) {
            title = '修改分类';
            var getUrl = $(this).attr('href');
            $.ajaxSettings.async = false;
            $.get(getUrl, function(result){
                result = $.parseJSON(result);
                var id = result.item.id;
                var name = result.item.name;
                layer_data = {
                    modifyId: id,
                    name: name,
                    dataUrl: '/sys/information/help/type/update'
                };
            });
        }
        else if ($(this).hasClass('delete')) {
            var dataUrl = $(this).attr('href');
            layer.confirm('您确定要删除此分类吗？', {
                title: '删除分类',
                skin: 'orangeLayer'
            }, function (index) {
                layer.msg('请稍候...', {icon: 16,time: 0});
                $.get(dataUrl, function (result) {
                    var res = $.parseJSON(result);
                    if (res.succ) {
                        layer.msg('删除成功', {
                            time: 1000
                        }, function(){
                            window.location.reload();
                        });
                    }
                    else {
                        layer.msg(res.msg);
                    }
                }).error(function () {
                    layer.msg('系统或参数错误!!!');
                });
                layer.close(index);
            }, function () {

            });
            return false;
        }
        else {
            return false;
        }
        temp = template('classifyModule', layer_data);
        layer.open({
            title: title,
            type: 1, //页面层需要手动关闭
            area: ['300px', '180px'],
            closeBtn: 1,
            content: temp,
            success: function (index) {
                $('#classifyForm').validate({
                    rules: {
                        name: "required",
                        roleName: "required"
                    },
                    messages: {
                        name: "分类名称不能为空",
                        roleName: "创建人不能为空"
                    },
                    submitHandler: function (form) {
                        if (document.all) {
                            event.returnValue = false;
                        } else {
                            e.preventDefault();
                        };
                        var dataUrl = $(form).attr('action');
                        var data = $(form).serialize();
                        layer.msg('请稍候...', {icon: 16,time: 0});
                        $.post(dataUrl, data, function(result){
                            result = $.parseJSON(result);
                            if( result.succ ) {
                                layer.msg('操作成功', {
                                    time: 1000
                                }, function(){
                                    window.location.reload();
                                });
                            }
                            else {
                                layer.msg(result.msg);
                            }
                        }).error(function(){
                            layer.msg('系统或参数错误!!!');
                        });
                    }
                });
            }
        });
        return false;
    });

});

//帮助列表弹窗的图片上传事件
function showImgInfo(obj) {
    var file = $(obj).get(0).files[0];
    var fileSize = 0;
    fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
    $('.img_info').text(file.name + ' / ' + fileSize);
}
//帮助列表弹窗的图片删除事件
function delImg() {
    $('#uploadImg').after($('#uploadImg').clone().val(""));
    $('#uploadImg').remove();
    $('#titleFileShow').val('');
    $('#titleFile').val('');
    $('.img_info').text('');
    $('.img_view').attr('src','');
}
//帮助列表弹窗的图片预览事件
function preview() {
    if ($(".img_view").attr("src")){
        layer.open({
            type: 1,
            title: false,
            closeBtn: true,
            //area: ['516px', '620px'],
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: $(".img_view")
        });
    }
}


//级联菜单事件
$('.selTypeBtn').click(function(){
    var x = $(this).offset();
    var top = x.top + 30;
    var left = x.left;
    $('.typeMenu').slideToggle();
    $('.typeMenu').offset({top: top, left: left});
    $('.subTypeMenu').hide();
    return false;
});
$('.selTypeBtn2').click(function(){
    var x = $(this).offset();
    var top = x.top + 30;
    var left = x.left;
    $('.propertyMenu').slideToggle();
    $('.propertyMenu').offset({top: top, left: left});
    return false;
});
//资讯列表分类选择
$('.caseMenu').on('click', 'a', function(){
    var typeId = $(this).attr('id');
    $('#typeId').val(typeId);

    if( typeId == 34 || typeId == 35 ) {
        var key = 13;
    }
    else if( typeId == 37 || typeId == 38 ) {
        var key = 36;
    }
    if( key != undefined ) {
        $.getJSON('/sys/information/article/type/child/'+key, function(result){
            console.log(result);
            var li = '';
            var resArr = result.items;
            if( resArr.length > 0 ) {
                $('.selTypeBtn2 span').first().text('请选择');
                $('.selTypeBtn2').css('display', 'inline-block');
                $.each(resArr, function(k, v){
                    li += "<li><a id='"+v.id+"' href=''>"+v.name+"</a></li>";
                });
                $('#propertyMenu ul').html(li);
            }
            else {
                $('.selTypeBtn2').css('display', 'none');
                $('.propertyMenu').slideUp();
            }
        }).error(function(){
            layer.msg('系统或网络异常');
        });
    }
    else {
        $('.selTypeBtn2').css('display', 'none');
        $('.propertyMenu').slideUp();
    }
    $('.selTypeBtn span').first().text($(this).text());
    $('.typeMenu').slideUp();
    $('.subTypeMenu').hide();
    return false;
});
//资讯列表分类选择
$('.propertyMenu').on('click', 'a', function(){
    var typeId = $(this).attr('id');
    $('#property').val(typeId);
    $('.selTypeBtn2 span').first().text($(this).text());
    $('.propertyMenu').slideUp();
    return false;
});
//帮助列表分类选择
$('.helpListTypeMenu').on('click', 'a', function(){
    var typeId = $(this).attr('id');
    $('#typeId').val(typeId);
    $('.selTypeBtn span').first().text($(this).text());
    $('.typeMenu').slideUp();
    $('.subTypeMenu').hide();
    return false;
});

$('.sel_class_module').hover(function(){
},function(){
    $('.subTypeMenu').hide();
});

function selectType(obj) {
    var divWidth = $(obj).parent().parent().parent().width();
    var x = $(obj).offset();
    var top = x.top;
    var left = x.left + divWidth + 10;
    var selfFloor = parseInt($(obj).attr('data-id'));
    var nextFloor = selfFloor + 1;
    var parentId = $(obj).attr('id');
    $('#subTypeMenu'+selfFloor).nextAll().hide();
    $('#subTypeMenu'+nextFloor).find('a').css('display', 'none');
    if( $('.subType_'+parentId).length > 0 ) {
        $('.subType_'+parentId).css('display','block');
        $('#subTypeMenu'+nextFloor).css('padding', '10').show().offset({top: top, left: left});
    }
    //帮助分类列表里的弹窗里的分类事件处理
    $('#tempsubTM'+selfFloor).nextAll().hide();
    $('#tempsubTM'+nextFloor).find('a').css('display', 'none');
    if( $('.subType_'+parentId).length > 0 ) {
        $('.subType_'+parentId).css('display','block');
        $('#tempsubTM'+nextFloor).css('padding', '10').show().offset({top: top, left: left});
    }
}
function stopPropagation(e) { //阻止事件冒泡
    if (e.stopPropagation)
        e.stopPropagation();
    else
        e.cancelBubble = true;
}
$(document).bind('click',function(){
    $('.typeMenu').slideUp();
    $('.subTypeMenu').hide();
    $('.propertyMenu').slideUp();
});
